package com.bs.process.project.controller;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import com.bs.common.utils.StringUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bs.common.annotation.Log;
import com.bs.common.enums.BusinessType;
import com.bs.process.project.domain.SysProject;
import com.bs.process.project.service.ISysProjectService;
import com.bs.system.domain.SysUser;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.framework.util.ShiroUtils;
import com.bs.common.core.page.TableDataInfo;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工地项目Controller
 *
 * @author Xianlu Tech
 * @date 2020-06-13
 */
@Controller
@RequestMapping("/process/project")
public class SysProjectController extends BaseController {
    private String prefix = "process/project/project";
    private String file_prefix = "process/project/file";

    @Autowired
    private ISysProjectService sysProjectService;

    @RequiresPermissions("process:project:view")
    @GetMapping("/project")
    public String project(ModelMap mmap) {
        SysUser sysUser = ShiroUtils.getSysUser();
        mmap.put("currentUser", sysUser);
        return prefix + "/project";
    }

    @GetMapping("/upfiles/{projectId}")
    public String upfiles(@PathVariable("projectId") Long proId, ModelMap mmap) {
        SysProject project = sysProjectService.selectSysProjectById(proId);
        mmap.put("sysProject", project);
        return file_prefix + "/upfiles";
    }

    @GetMapping("/projectDetails/{projectId}")
    public String projectDetails(@PathVariable("projectId") Long proId, ModelMap mmap) {
        SysProject project = sysProjectService.selectSysProjectById(proId);
        mmap.put("sysProject", project);
        return prefix + "/projectDetails";
    }

    /**
     * 查询工地项目列表
     */
    @RequiresPermissions("process:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProject sysProject) {
        startPage();
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        return getDataTable(list);
    }

    /**
     * 导出工地项目列表
     */
    @RequiresPermissions("process:project:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysProject sysProject) {
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        ExcelUtil<SysProject> util = new ExcelUtil<SysProject>(SysProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 新增工地项目
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工地项目
     */
    @RequiresPermissions("process:project:add")
    @Log(title = "工地项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysProject sysProject) {
        return toAjax(sysProjectService.insertSysProject(sysProject));
    }

    /**
     * 修改工地项目
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        SysProject sysProject = sysProjectService.selectSysProjectById(projectId);
        mmap.put("sysProject", sysProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存工地项目
     */
    @RequiresPermissions("process:project:edit")
    @Log(title = "工地项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysProject sysProject) {
        return toAjax(sysProjectService.updateSysProject(sysProject));
    }

    /**
     * 删除工地项目
     */
    @RequiresPermissions("process:project:remove")
    @Log(title = "工地项目", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysProjectService.deleteSysProjectByIds(ids));
    }

    @RequestMapping("/downQrCode/{pId}")
    @ResponseBody
    public void downQrCode(@PathVariable("pId") Long pId, HttpServletResponse response) {
        SysProject sysProject = sysProjectService.selectSysProjectById(pId);
        if (sysProject == null || StringUtils.isEmpty(sysProject.getQrCode())) {
            return;
        }

        String qrCode = sysProject.getQrCode();
        BASE64Decoder decoder = new BASE64Decoder();

        try {
            byte[] bytes = decoder.decodeBuffer(qrCode);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }

            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            String filename = sysProject.getProjectName() + ".jpg";
            filename = URLEncoder.encode(filename, "UTF-8");
            response.setContentType("image/jpeg");
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            IOUtils.copy(in, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
